<template>
  <div class="container">
    <div class="app-container">
      <el-button class="btn-add" size="mini" type="primary" @click="addZero">添加权限</el-button>
      <el-table default-expand-all :data="list" row-key="id">
        <el-table-column label="名称" prop="name" />
        <el-table-column label="标识" prop="code" />
        <el-table-column label="描述" prop="description" />
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button v-if="row.type===1" size="mini" type="text" @click="addOne(row.id)">添加</el-button>
            <el-button size="mini" type="text" @click="doEdit(row.id)">编辑</el-button>
            <el-button size="mini" type="text" @click="doDel(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog :title="isEdit? '编辑权限':'添加权限'" :visible="showDialog" @close="hClose">
      <el-form ref="form" label-width="120px" :model="formData" :rules="rules">
        <el-form-item prop="name" label="权限名称">
          <el-input v-model="formData.name" placeholder="2-10个字符" style="width: 80%" size="mini" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" placeholder="2-10个字符" style="width: 80%" size="mini" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="formData.description" placeholder="1-100个字符" type="textarea" size="mini" :rows="4" style="width: 80%" />
        </el-form-item>
        <el-form-item label="开启">
          <el-switch
            v-model="formData.enVisible"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>
        <el-form-item>
          <!-- 按钮 -->
          <el-row type="flex" justify="center">
            <el-col :span="12">
              <el-button size="mini" type="primary" @click="isOK">确定</el-button>
              <el-button size="mini">取消</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
// import AddPermission from './components/add-permission'
import { getPermissionList, addPermission, updatePermission, getPermissionDetail, delPermission } from '@/api/permission'
// import { getDepartment } from '@/api/department'
import { transListToTreeData } from '@/utils/index'
export default {
  name: 'Permission',
  // components: { AddPermission },
  data() {
    return {
      list: [],
      isEdit: false,
      showDialog: false,
      currentNodeId: 0,
      type: '',
      formData: {
        name: '', // 权限名称
        code: '', // 权限编码
        description: '', // 权限介绍
        pid: '', // 父级权限的id,
        type: '',
        enVisible: ''
      },
      rules: {
        code: [{ required: true, message: '权限编码不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '权限编码的长度为2-10个字符', trigger: 'blur' },
          {
            validator: async(rule, value, callback) => {
              const res = await getPermissionList()
              let data = res.data
              if (this.isEdit) {
                data = res.data.filter(item => item.code !== this.formData.code
                )
              }
              if (data.find(item => item.code === value)) {
                callback(new Error('提示标识[' + value + ']已经占用'))
              } else {
                callback()
              }
            }, trigger: 'blur'
          }
        ],
        description: [
          { required: true, message: '权限介绍不能为空', trigger: 'blur' },
          { min: 1, max: 100, message: '权限介绍的长度为1-100个字符', trigger: 'blur' }
        ],
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' },
          {
            min: 2, max: 10, message: '权限名称的长度为2-10个字符', trigger: 'blur'
          },
          {
            validator: async(rule, value, callback) => {
              const res = await getPermissionList()
              let data = res.data
              if (this.isEdit) {
                data = res.data.filter(item => item.name !== this.formData.name
                )
              }
              if (data.find(item => item.name === value)) {
                callback(new Error('提示标识[' + value + ']已经占用'))
              } else {
                callback()
              }
            }, trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    this.loadPermission()
  },
  methods: {
    async loadPermission() {
      const res = await getPermissionList()
      console.log(res.data)
      this.list = transListToTreeData(res.data, 0)
    },
    addZero() {
      this.showDialog = true
      this.formData.type = 1
      this.formData.pid = 0
    },
    hClose() {
      this.showDialog = false
      this.$refs.form.resetFields()
      this.formData = {
        name: '', // 权限名称
        code: '', // 权限编码
        description: '', // 权限介绍
        pid: '', // 父级权限的id,
        type: '',
        enVisible: ''
      }
    },
    isOK() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          let res
          if (!this.isEdit) {
            res = await addPermission(this.formData)
            this.$message.success(res.message)
          } else {
            res = await updatePermission(this.formData)
            console.log(res)
            this.isEdit = false
          }
          this.$message.success(res.message)
          this.hClose()
          this.loadPermission()
        }
      })
    },
    addOne(pid) {
      this.showDialog = true
      this.formData.type = 2
      this.formData.pid = pid
    },
    async doEdit(id) {
      const res = await getPermissionDetail(id)
      this.isEdit = true
      this.showDialog = true
      this.formData = res.data
    },
    async doDel(id) {
      const res = await delPermission(id)
      this.$message.success(res.message)
      this.hClose
      this.loadPermission()
    }
  }
}
</script>
<style>
.btn-add {
  margin: 10px;
}
</style>
